<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 8/9/2018
 * Time: 8:22 PM
 */
namespace Admin\Model;

use Think\Model;

class ManagerModel extends Model
{
    //模型对应的表名
    protected $trueTableName = 'managers';

    public function login($account, $password)
    {
        $where['account'] = $account;
        $manager = $this->where($where)->find();

        if (empty($account)) {
             $this->error = '输入账号不能为空';
             return false;
        }

        if (empty($password)) {
             $this->error = '密码不能为空';
             return false;
        }

        if (empty($manager)) {
             $this->error = '账号不存在';
             return false;
        }

        if ($manager['status'] == '0') {
             $this->error = '您的账号被限制登录';
             return false;
        }

        if ($manager['password'] != md5(C('salt').$password)) {
             $this->error = '密码不正确';
             return false;
        }

        //将登录的用户信息存入session
        session('id', $manager['id']);
        session('account', $manager['account']);
        session('account_info', json_encode($manager));

        return true;
    }

    public function changePassword($old, $new, $length=6)
    {
        $account = session('account');
        $manager = $this->where(array('account' => array('eq', $account)))->find();

        if (empty($old)) {
            $this->error = '原始密码不能为空';
            return false;
        }
        if ($manager['password'] != md5(C('salt').$old)) {
            $this->error = '原始密码不正确';
            return false;
        }
        if (empty($new)) {
            $this->error = '新密码不能为空';
            return false;
        }
        if (strlen($new) < $length) {
            $this->error = '密码长度不能小于'.$length.'位';
            return false;
        }
        $this->password = md5(C('salt').$new);
        $this->save();
        return true;
    }

    //增加管理员
    public function managerAdd()
    {
        $account = I('post.account', '');
        $password = I('post.password');
        $status = I('post.status', '0');
        if (empty($account)) {
            $this->error = '账号不能为空';
            return false;
        }

        $count = $this->where(array('account' => array('eq', $account)))->count();
        if ($count > 0) {
            $this->error = '账号名已经存在';
            return false;
        }
        if (empty($password)) {
            $this->error = '密码不能为空';
            return false;
        }
        if (strlen($password) < 6) {
            $this->error = '密码长度必须大于6位';
            return false;
        }

        $this->account = $account;
        $this->password = md5(C('salt').$password);
        $this->is_default = 1;
        $this->status = $status;
        $this->add();

        return true;
    }

    public function managerModify($id)
    {
        //先查出原来的账号
        $manager = $this->where(array('id' => array('eq',$id)))->find();

        $account = I('post.account');
        $password = I('post.password');
        $status = I('post.status', '0');

        $now_manager = $this->where(array('id' => array('eq', session('id'))))->find();
        if ($now_manager['is_default'] == 1) {
            $this->error = '您没有权限修改管理员账户';
            return false;
        }

        if ($status == 0 and session('id') == $id) {
            $this->error = '您不能修改自己账号的为未激活的状态';
            return false;
        }

        if (empty($account)) {
            $this->error = '账号不能为空';
            return false;
        }

        $count = $this->where(array('account' => array('eq', $account), 'id' => array('neq', $id)))->count();
        if ($count > 0) {
            $this->error = '账号名已经存在';
            return false;
        }
        if (empty($password)) {
            $password = $manager['password'];
        } else {
            $password = md5(C('salt').$password);
        }
        if (strlen($password) < 6) {
            $this->error = '密码长度必须大于6位';
            return false;
        }

        $this->account = $account;
        $this->password = $password;
        $this->is_default = 1;
        $this->status = $status;
        $this->save();

        return true;
    }
}